﻿package yii.db.ar{

	import phpas3.*;
	import yii.db.*;
	import yii.db.schema.*;
	import flash.utils.*;

	public class CActiveRecordMetaData{

		private var _model:CActiveRecord;
		public var tableSchema:CDbTableSchema;
		public var columns:Object;
		public var attributeDefaults:Array = new Array();
		public var relations:Object = new Object();

		public function CActiveRecordMetaData( $model:CActiveRecord ):void{
                this._model = $model;

                var $tableName:String = $model.tableName();
                var $table:CDbTableSchema = $model.getDbConnection().getSchema().getTable( $tableName );
                if( $table === null ){
                	throw new Error( "ARクラス「"+ get_class($model) +"」に対応するテーブル「"+ $tableName +"」がデータベースに見つかりませんでした" );
                	//throw new Error( 'The table '+ $tableName +' for active record class '+ get_class($model) +' cannot be found in the database.' );
                }
                if($table.primaryKey===null){
                        $table.primaryKey = $model.primaryKey();
                        if( is_string($table.primaryKey) && isset($table.columns[$table.primaryKey]) ){
                                $table.columns[$table.primaryKey].isPrimaryKey=true;
                        } else if(is_array($table.primaryKey)){
                                for( var index in $table.primaryKey ){
                                		var $name:String = $table.primaryKey[index];
                                        if(isset($table.columns[$name])){
                                                $table.columns[$name].isPrimaryKey = true;
                                        }
                                }
                        }
                }
                this.tableSchema = $table;
                this.columns = $table.columns;

                for( var $name in $table.columns ){
                		var $column = $table.columns[$name];
                        if(!$column.isPrimaryKey && $column.defaultValue!==null){
                                this.attributeDefaults[$name]=$column.defaultValue;
                        }
                }
                for( $name in $model.relations() ){
                		var $config = $model.relations()[$name];
                        this.addRelation($name,$config);
                }
         }







		public function addRelation( $name, $config ){
			if( isset($config[0],$config[1],$config[2]) ){  // relation class, AR class, FK
				var klass:Class = getDefinitionByName( $config[0] ) as Class;
				this.relations[$name] = new klass( $name, $config[1], $config[2], array_slice($config,3) );
			} else {
				//throw new CDbException(Yii::t('yii','Active record "{class}" has an invalid configuration for relation "{relation}". It must specify the relation type, the related active record class and the foreign key.', array('{class}'=>get_class($this->_model),'{relation}'=>$name)));
			}
		}






	}
}